<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1.冒泡排序
        console.log('1.冒泡排序')
        var arr1 = [1, 5, 2, 6, 4, 5, 2, 58, 2, 65, 5, 2, 5]
        console.log(arr1)
        for (var i = 0; i < arr1.length; i++) {
            for (var j = 0; j < arr1.length - i; j++) {
                if (arr1[j] > arr1[j + 1]) {
                    var tem = arr1[j]
                    arr1[j] = arr1[j + 1]
                    arr1[j + 1] = tem
                }
            }
        }
        console.log(arr1)

        // 2.数组随机排序
        // 1.sort排序
        var arr2 = [1, 59, 5, 94, 59, 48, 48, 48, 4, 4, 51, 5]
        var newarr2 = [...arr2]
        var newarr2 = newarr2.sort((a, b) => {
            return Math.random() - 0.5
        })
        console.log('2.1.数组随机排序')
        console.log(arr2)
        console.log(newarr2)
        // 2.随机洗牌
        var newarr3 = [...arr2]
        var length = arr2.length
        for (var i = 0; i < length; i++) {
            const index = Math.floor(Math.random() * length)
            var tem = newarr3[index]
            newarr3[index] = newarr3[i]
            newarr3[i] = tem
        }
        console.log('2.2.随机洗牌排序')
        console.log(arr2)
        console.log(newarr3)

        // 3.已知数组 var stringArray = [“This”,“is”, “Baidu”,“Campus”],
        // Alert 出”This is Baidu Campus”
        var arr3 = ['This', 'is', 'Baidu', 'Campus']
        var newarr3 = arr3.join('')
        console.log('3.3拼接字符串')
        console.log(newarr3)


        // 4.手些闭包
        function bibao() {
            const a = 1
            const b = 2
            function neibu() { return a + b }
            return neibu
        }

        // 5.封装一个函数，格式化日期对象，返回yyyy-MM-dd HH:mm:ss的形式
        function datachange(data) {
            if (!(data instanceof Date)) return console.log('data不是日期对象')

            var year = data.getFullYear()
            var month = data.getMonth() + 1
            var day = data.getDate()
            var hour = data.getHours()
            var minute = data.getMinutes()
            var second = data.getSeconds()

            month = month < 10 ? '0' + month : month
            day = day < 10 ? '0' + day : day
            hour = hour < 10 ? '0' + hour : hour
            minute = minute < 10 ? '0' + minute : minute
            second = second < 10 ? '0' + second : second

            return year + '-' + month + '-' + day + '-' + hour + '-' + minute + '-' + second
        }

        var data = new Date()
        console.log('5.封装日期函数')
        console.log(data)
        console.log(datachange(data))

        //  6.封装一个函数，获取对应文件的后缀名 https://www.test.com/login/index.html
        //获取url中 的用户名和密码 https://www.test.com/login?name=zs&pwd=123; 
        var url = 'https://www.test.com/login?name=zs&pwd=123'
        function getparm(url) {
            var index = url.indexOf('?') + 1
            var parm = url.substr(index)
            var arr = parm.split('&')
            var o = {}
            for (var i = 0; i < arr.length; i++) {
                var user = arr[i].split('=')
                var key = user[0]
                var value = user[1]
                o[key] = value
            }
            return o
        }
        var obj = getparm(url)
        console.log('6.截取url后的账户密码')
        console.log(obj)

        // 7.'qweqweoeqweroqweqweodfsfdo' 查找字符串中所有字母 'o'出现的位置
        //  把字符串中的所有字母'o'替换成 '-';
        console.log('7.查找o出现的位置并替换位o')
        var str = 'qweqweoeqweroqweqweodfsfdo'
        var index = -1
        var p = 1
        do {
            var index = str.indexOf('o', index + 1)
            if (index !== -1) {
                console.log('第' + p + '个字母出现在第' + index + '个位置')
                p++
            }
        } while (index !== -1)
        do {
            var index = str.indexOf('o', index + 1)
            if (index !== -1) {
                str = str.replace('o', '-')
            }
        } while (index !== -1)
        console.log(str)

        // 8.将两个数组合并并删除第二个字符
        var ar1 = [1, 2, 3]
        var ar2 = [4, 5, 6]
        var newar = ar1.concat(ar2)
        console.log('8.合并并删除第二个元素')
        newar.splice(1, 1)
        console.log(newar)

        // 9.清楚所有空格
        var tes = '   2151    1515  1515'
        console.log('9.清楚前后空格')
        console.log(tes.trim())

        // 10.截取一定字符串之后
        var tef = 'abcdefg'
        var tef2 = tef.substring(4)
        console.log('10.截取第几位之后')
        console.log(tef2)

        // 11.判断一个字符串中出现次数最多的字符，统计这个次数
        var test6 = 'abcoefoxyozzopp'
        // 统计出现字数的字符
        var m
        // 次数
        var ch = 0
        var g = {}
        for (var i = 0; i < test6.length; i++) {
            var item = test6.charAt(i)
            if (g[item]) {
                g[item]++
            } else {
                g[item] = 1
            }
        }
        for (key in g) {
            if (ch < g[key]) {
                m = key
                ch = g[key]
            }
        }
        console.log('11.统计出现最多的字符和次数')
        console.log(g)
        console.log('出现最多是是：' + m)
        console.log('初选最多的次数位：' + ch)

        // 12.深拷贝
        var obj1 = {
            name: '胖鸡',
            age: 18,
            num: 66
        }
        var obj2 = {}
        for (key in obj1) {
            obj2[key] = obj1[key]
        }
        console.log('12.数组去重')
        console.log(obj2)

        // 13.数组去重
        var arr = [1, 2, 1, 1, '1', 3, 3];
        const unique = function (arr) {
            var obj = {}
            var res = []
            arr.forEach(item => {
                if (!obj[item]) {
                    obj[item] = true
                    res.push(item)
                }
            })
            return res
        }
        console.log('13.数组去重')
        console.log(unique(arr)); // [1, 2, 3]

        // 5.用 js 实现随机选取 10–100 之间的 10 个数字，存入一个数组并排序

    </script>
</body>

</html>